<template>
<el-dialog
    :title="$t('user.add_level')"
    v-model="dialogVisible"
    @close="dialogFormVisible"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    width="600px"
  >
    <el-form size="small" :model="form" ref="form">
      <el-form-item
        :label="$t('user.level_name')"
        :label-width="formLabelWidth"
        prop="name"
        :rules="[{  required: true,  message: $t('user.input_level_name')}]"
      >
        <el-input v-model="form.name" :placeholder="$t('user.input_level_name')"></el-input>
      </el-form-item>
      <el-form-item
        :label="$t('user.level_weight')"
        :label-width="formLabelWidth"
        prop="weight"
        :rules="[{  required: true,  message: $t('user.input_level_weight')}]"
      >
        <el-input
          v-model="form.weight"
          type="number"
          :placeholder="$t('user.input_level_weight')"
        ></el-input>
        <div class="gray9">{{$t('user.weight_tip')}}</div>
      </el-form-item>
      <el-form-item
        :label="$t('user.level_discount')"
        :label-width="formLabelWidth"
        prop="equity"
        :rules="[{  required: true,  message: $t('user.input_level_discount')}]"
      >
        <el-input
          type="number"
          :precision="1"
          :step="1"
          :min="0"
          :max="100"
          :placeholder="$t('user.input_level_discount')"
          v-model="form.equity"
        >
          <template #append>%</template>
        </el-input>
      </el-form-item>
      <el-form-item
        :label="$t('user.reward_points')"
        :label-width="formLabelWidth"
        prop="givePoints"
        :rules="[{  required: true,  message: $t('user.input_reward_points')}]"
      >
        <el-input
          v-model="form.givePoints"
          type="number"
          :placeholder="$t('user.input_reward_points')"
        ></el-input>
        <div class="gray9">{{$t('user.upgrade_reward_points')}}</div>
      </el-form-item>
      <el-form-item :label="$t('user.upgrade_conditions')" :label-width="formLabelWidth">
        <div class="gray9"> {{$t('user.upgrade_condition_tip')}} 

</div>
        <div class="d-s-c mt16">
          <el-checkbox v-model="form.openMoney">{{$t('user.total_spent_reach')}}</el-checkbox>
          <el-input
            v-model="form.upgradeMoney"
            type="number"
            :disabled="form.openMoney == 0"
            style="width: 160px; margin-left: 10px"
          ></el-input>
          <span class="ml10">{{$t('user.currency_yuan')}}</span>
        </div>
        <div class="d-s-c mt16">
          <el-checkbox v-model="form.openPoints">{{$t('user.total_points_reach')}}</el-checkbox>
          <el-input
            v-model="form.upgradePoints"
            type="number"
            :disabled="form.openPoints == 0"
            style="width: 160px; margin-left: 10px"
          ></el-input>
          <span class="ml10">{{$t('user.units')}}</span>
        </div>
        <div class="d-s-c mt16">
          <el-checkbox v-model="form.openInvite">{{$t('user.referrals_reach')}}</el-checkbox>
          <el-input
            v-model="form.upgradeInvite"
            type="number"
            :disabled="form.openInvite == 0"
            style="width: 160px; margin-left: 10px"
          ></el-input>
          <span class="ml10">{{$t('user.people')}}</span>
        </div>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogFormVisible">{{$t('user.cancel')}}</el-button>
        <el-button type="primary" @click="addGrade()" :disabled="submit_loading"
          >{{$t('user.confirm')}}</el-button
        >
      </div>
    </template>
  </el-dialog>
</template>

<script>
import UserApi from "@/api/user.js";
export default {
  data() {
    return {
      form: {
        /*昵称*/
        name: "",
        /*等级折扣*/
        equity: "",
        /*是否打开累计消费满*/
        openMoney: 0,
        /*累计消费满*/
        upgradeMoney: 0,
        /*是否打开累计积分满*/
        openPoints: 0,
        /*累计积分满*/
        upgradePoints: 0,
        /*是否推荐人数满*/
        openInvite: 0,
        /*推荐人数满*/
        upgradeInvite: 0,
        /*等级权重*/
        weight: 100,
        givePoints: 0
      },
      /*左边长度*/
      formLabelWidth: "120px",
      /*是否显示*/
      dialogVisible: false,
      /*是否正在提交*/
      submit_loading: false
    };
  },
  props: ["open_add"],
  created() {
    this.dialogVisible = this.open_add;
  },
  methods: {
    /*添加等级*/
    addGrade() {
      let self = this;
      let params = this.form;
      self.$refs.form.validate(valid => {
        if (valid) {
          self.submit_loading = true;
          params.openMoney = params.openMoney == true ? 1 : 0;
          params.openPoints = params.openPoints == true ? 1 : 0;
          params.openInvite = params.openInvite == true ? 1 : 0;
          UserApi.addgrade(params, true).then(data => {
            self.submit_loading = false;
            ElMessage({
              message: data.msg,
              type: "success"
            });
            self.dialogFormVisible(true);
          }).catch(error => {
            self.submit_loading = false;
          });
        }
      });
    },
    /*关闭弹窗*/
    dialogFormVisible(e) {
      if (e) {
        this.$emit("closeDialog", {
          type: "success",
          openDialog: false
        });
      } else {
        this.$emit("closeDialog", {
          type: "error",
          openDialog: false
        });
      }
    }
  }
};
</script>

